package cn.kgc.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @author Rock
 * @create 2020-08-12 16:51
 */
@Component
public class MailUtil {

    @Value("${spring.mail.username}")
    private String from;

    @Autowired
    private JavaMailSender javaMailSender;

    /**
     * 发送文本邮件
     * @param to 收件人
     * @param subject 邮件标题
     * @param context 邮件内容
     */
    public void send(String to,String subject,String context){
        try {
            //1.创建mimeMessage
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            //2.创建mimeMessageHelper
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
            //3.添加消息体并发送
            //发件人
            helper.setFrom(from);
            //收件人
            helper.setTo(to);
            //右键标题
            helper.setSubject(subject);
            //右键内容
            helper.setText(context);
            //发送
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
    /**
     *发送带附件的邮件
     * @param to 收件人
     * @param subject 邮件标题
     * @param context 邮件内容
     * @param filepath 附件地址
     */
    public void send2(String to,String subject,String context,String filepath){
        try {
            //1.创建mimeMessage
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            //2.创建mimeMessageHelper
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
            //3.添加消息体并发送
            //发件人
            helper.setFrom(from);
            //收件人
            helper.setTo(to);
            //右键标题
            helper.setSubject(subject);
            //右键内容
            helper.setText(context);
            //附件
            FileSystemResource fileSystemResource = new FileSystemResource(new File(filepath));
            //解析文件名
            String fileName = filepath.substring(filepath.lastIndexOf(File.separator));
            helper .addAttachment(fileName,fileSystemResource);
            //发送
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
    /**
     *发送html样式的的邮件
     * @param to 收件人
     * @param subject 邮件标题
     * @param context 邮件内容
     */
    public void send3(String to,String subject,String context){
        try {
            //消息处理类
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
            //发件人
            helper.setFrom(from);
            //收件人
            helper.setTo(to);
            //邮件标题
            helper.setSubject(subject);
            //邮件内容
            helper.setText(context,true);
            //发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
